package com.sign2024.sign2024project.projectservice.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.projectservice.PlanInventory;
import com.sign2024.sign2024project.projectservice.mapper.PlanInventoryMapper;
import com.sign2024.sign2024project.projectservice.param.PlanInventoryParam;
import com.sign2024.sign2024project.projectservice.service.PlanInventoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PlanInventoryServiceImpl implements PlanInventoryService {

    @Resource
    private PlanInventoryMapper planInventoryMapper;


    @Override
    public void excelImportData(List<PlanInventory> list, Integer taskId) {
        if (StringUtils.isEmpty(list)) {
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        } else {
            // 批量 添加 计划清单
            planInventoryMapper.insertBatchPlanInventoryByTaskId(list, taskId);
        }
    }

    @Override
    public boolean deleteByTaskId(Integer taskId) {
        int total = planInventoryMapper.deleteByTaskId(taskId);
        return total > 0;
    }

    @Override
    public Page<PlanInventory> pagePlanInventory(PlanInventoryParam param) {
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<PlanInventory> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<PlanInventory> planInventories = planInventoryMapper.selectPlanInventoryByPage(param);

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = planInventoryMapper.pagePlanInventoryCount(param);
        page.setTotal(aLong);
        page.setRecords(planInventories);

        return page;
    }
}